﻿//#pragma execution_character_set("utf-8")

#include "rm_logindialog.h"
#include "ui_rm_logindialog.h"
#include <QStyle>
#include <QSettings>
#include "mainwindow.h"
#include "../rmutils.h"

rm_LoginDialog::rm_LoginDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::rm_LoginDialog)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("清华同方云视频会议"));

    QSettings settings;
    QStringList userlist = settings.value("users",QStringList("")).toStringList();
    ui->comboBox->addItems(userlist);

    ui->lineEdit_ip->hide();
    ui->lineEdit_port->hide();
}

rm_LoginDialog::~rm_LoginDialog()
{
    delete ui;
}

void rm_LoginDialog::login()
{
    QStringList loginparams;
    QString phone = ui->comboBox->currentText();
    QString pwd = ui->lineEdit_pwd->text();

    QJsonObject loginobj;
    loginobj.insert("type","Type_Login");
    loginobj.insert("phone",phone);
    loginobj.insert("pwd",pwd);


    MainWindow::userphone=phone;
    MainWindow::g_MainWin->m_webSocket.sendTextMessage(RMUtils::JsonToQstring(loginobj));

}

void rm_LoginDialog::on_pushButton_login_clicked()
{
    QString host = ui->lineEdit_ip->text();
    QString port = ui->lineEdit_port->text();

    MainWindow::g_MainWin->m_webSocket.open(QUrl(host+port));
    MainWindow::serverip=host.remove("ws://");
}

void rm_LoginDialog::login_success(QJsonObject obj)
{

    QSettings settings;
    QStringList userlist = settings.value("users",QStringList("")).toStringList();
    if(!userlist.contains(ui->comboBox->currentText()))
    {
        userlist.push_front(ui->comboBox->currentText());
        settings.setValue("users",userlist);
    }

}

void rm_LoginDialog::login_failed(QString)
{

}
